<?php
class Category_controller extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();

		// load helper
		$this->load->helper("url");

		// load model and connect database
		$this->load->model("Category_model", '', TRUE);
	}

	public function validation()
	{
			$this->load->view('validation');
	}

	public function test()
	{
		$this->load->view('test');
	}

	public function homepage()
	{
		$this->load->view('homepage');
	}	

	public function lightbox($language='ens')
	{
		if ($language == "ens") {
			$this->load->view('ens-lightbox');
			$this->load->view('snapshoot');
		}		
		else if ($language == "vni") {
			$this->load->view('vni-lightbox');
			$this->load->view('snapshoot');
		}
	}

	public function source()
	{
		$data = $this->Category_model->getAllNames();

		echo json_encode($data);
	}

	public function search()
	{
		$data['results'] = $this->Category_model->getAllNames();

		// $data['json'] = json_encode($data);
		$this->load->view('category/demotypehead', $data);
	}	

	public function comment()
	{
		$data['results'] = $this->Category_model->listall();

		$this->load->view("category/commentAjax", $data);
	}

	public function insert_comment()
	{
		$this->load->helper('form');
		if (strcasecmp('XMLHttpRequest', $_SERVER['HTTP_X_REQUESTED_WITH']) === 0)
		{
   			// Ajax Request
   			$this->Category_model->AddCategory();
		}
		else
		{
			// $this->Category_model->AddCategory();
			echo '<h1>Not Ajax Request</h1>';
			// redirect('/Category_controller/index');
		}
		

		

	}

	public function index()
	{
		$data['results'] = $this->Category_model->listall();

		$this->load->view("category/index", $data);

		$this->load->view('category/create');
		$this->load->view('category/edit');
		$this->load->view('category/delete');
	}

	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$data['category'] = 'Create a news category';

		$this->form_validation->set_rules('categoryid', 'categoryid', 'required');
		$this->form_validation->set_rules('categoryname', 'categoryname', 'required');

		if ($this->form_validation->run() === FALSE)
		{
			$this->load->view('category/create');

		}
		else
		{
			$this->Category_model->AddCategory();
			redirect('/Category_controller/index');
		}
	}


	public function update($id)
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('categoryid', 'categoryid', 'required');
		$this->form_validation->set_rules('categoryname', 'categoryname', 'required');

		if ($this->form_validation->run() === FALSE)
		{
			$data['id'] = $id;
			$data['category'] = $this->Category_model->GetById($id)->row();

			$this->load->view('category/edit', $data);
		}
		else
		{
			$id = $this->input->post('categoryid');
			$category = array( 'id' => $id,
				'name' => $this->input->post('categoryname') );

			$this->Category_model->Update($id, $category);
			redirect('/Category_controller/index');
		}		
	}

	function delete($id)
	{
		// delete person
		$this->Category_model->delete($id);
		
		// redirect to person list page
		redirect('Category_controller/index/','refresh');
	}
}
?>